import axios from '@/libs/api.request'

export const login = ({username, password}) => {
  const data = {
    username,
    password
  }
  return axios.request({
    url: 'login',
    data,
    method: 'post'
  })
}

export const getUserInfo = (token) => {
  return axios.request({
    url: 'get_info',
    params: {
      token
    },
    method: 'get'
  })
}

export const logout = (token) => {
  return axios.request({
    url: 'logout',
    method: 'post'
  })
}

export const getUserList = (username, page, limit) => {
  return axios.request({
    url: 'sys/user/list',
    params: {
      username,
      page,
      limit
    },
    method: 'get'
  })
}

export const deleteUsers = (userIds) => {
  return axios.request({
    url: '/sys/user/delete',
    data: userIds,
    method: 'post'
  })
}

export const saveOrUpdateUser = (user, type) => {
  const url = type ? '/sys/user/save' : '/sys/user/update'
  return axios.request({
    url: url,
    data: user,
    method: 'post'
  })
}

/** 角色 */
export const getRoleList = (roleName, page, limit) => {
  return axios.request({
    url: 'sys/role/list',
    params: {
      roleName,
      page,
      limit
    },
    method: 'get'
  })
}

export const saveOrUpdateRole = (role, type, menus) => {
  const url = type ? '/sys/role/save' : '/sys/role/update'
  role.menuIdList = []
  menus.map(menu => role.menuIdList.push(menu.menuId))
  return axios.request({
    url: url,
    data: role,
    method: 'post'
  })
}

export const deleteRoles = (roleIds) => {
  return axios.request({
    url: '/sys/role/delete',
    data: roleIds,
    method: 'post'
  })
}

/** 菜单 */
export const getMenus = () => {
  return axios.request({
    url: '/sys/menu/list',
    method: 'get'
  })
}

export const getRoleMenus = (roleId) => {
  return axios.request({
    url: '/sys/role/info/' + roleId,
    method: 'get'
  })
}

export const onlyMenus = () => {
  return axios.request({
    url: '/sys/menu/select',
    method: 'get'
  })
}
